//
// Copyright (c) 2024 BPer
//
#include <bperrtp/BPerPublishNackBuffer.h>
#include <bperutil/sys/BPerLog.h>


void bPer_create_pubNackbuffer(BPerPublishNackBuffer* nackbuf,int32_t capacity){
	if(nackbuf==NULL) return;
	nackbuf->capacity = capacity;
	nackbuf->capacityIndex=capacity-1;
	nackbuf->queue =(BPerSendNackBuffer*) bPer_calloc(1,sizeof(BPerSendNackBuffer)*nackbuf->capacity);

}

void bPer_destroy_pubNackbuffer(BPerPublishNackBuffer* nackbuf){
	if(nackbuf==NULL) return;
	bPer_free(nackbuf->queue);
}


BPerSendNackBuffer* bPer_pubnackbuffer_at(BPerPublishNackBuffer* nackbuf,uint16_t seq) {
	if(nackbuf==NULL) return NULL;
    return &nackbuf->queue[seq&nackbuf->capacityIndex];
}

void bPer_pubnackbuffer_set(BPerPublishNackBuffer* nackbuf,uint16_t at, char* data,int plen)
{
	if(nackbuf==NULL||data==NULL) return;

	BPerSendNackBuffer *p1=&nackbuf->queue[at&nackbuf->capacityIndex];
	p1->payload=data;
	p1->nb=plen;
	p1->seq=at;
}

void bPer_pubnackbuffer_remove(BPerPublishNackBuffer* nackbuf,uint16_t at)
{
	if(nackbuf==NULL) return;
	bPer_pubnackbuffer_set(nackbuf,at, NULL,0);
}

